package com.wlhse.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.wlhse.dao.AndroidVersionDao;
import com.wlhse.dto.outDto.AndroidVersionOutDto;
import com.wlhse.service.AndroidVersionService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class AndroidVersionServiceImpl implements AndroidVersionService {

    @Resource
    private AndroidVersionDao androidVersionDao;

    @Resource
    private AndroidVersionOutDto androidVersionOutDto;

    public String getVersion(String version) {
        JSONObject object = new JSONObject(true);
        JSONObject object1 = new JSONObject(true);
        List<JSONObject> list = new ArrayList<>();
        androidVersionOutDto = androidVersionDao.queryAndroidVersion();
        //数据库中存在的版本信息
        if (androidVersionDao.queryAndroidVersion() != null) {
            //版本号不同
            if (!version.equals(androidVersionOutDto.getServicerSion())) {
                //第一位不同,第二位不同
                String[] strs1 = version.split("\\.");
                String[] strs2 = (androidVersionOutDto.getServicerSion()).split("\\.");
                //强制更新APP---版本号第一位不同
                if (!strs1[0].equals(strs2[0])) {
                    object1.put("updateUrl", androidVersionOutDto.getSqlAddress());
                    list.add(object1);
                    object.put("serverVersion", androidVersionOutDto.getServicerSion());
                    object.put("status", 3);
                    object.put("message", "强制更新App");
                    object.put("data", list);
                }
                //强制更新数据库---版本号第二位不同
                else if (!strs1[1].equals(strs2[1])) {
                    object1.put("updateSqlUrl", androidVersionOutDto.getSqlAddress());
                    list.add(object1);
                    object.put("serverVersion", androidVersionOutDto.getServicerSion());
                    object.put("status", 2);
                    object.put("message", "强制更新数据库");
                    object.put("data", list);
                } else if (!strs1[2].equals(strs2[2])) {
                    object.put("serverVersion", androidVersionOutDto.getServicerSion());
                    object.put("status", 1);
                    object.put("message", "正在服务");
                }
            } else {
                object.put("serverVersion", androidVersionOutDto.getServicerSion());
                object.put("status", 1);
                object.put("message", "正在服务");
            }
        }
        return object.toJSONString();
    }
}
